package nsoft.swing.window;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;

import nsoft.swing.engine.ContainerFactory;
import nsoft.util.UiModel;

public abstract class UiManager implements WindowListener {

	private JFrame appicationWindow;

	private MenuContext menuContext;

	private ContainerFactory builder;

	protected Map<String, UiModel> openViews = new HashMap<String, UiModel>();

	public abstract void init();

	public abstract UiType getUiType();

	/**
	 * To create Applicatio window view.
	 */
	// public abstract JMenuBar getMenuBar();
	// public abstract JToolBar getToolBar();
	public abstract void openWindow(String screenId, UiModel controller);

	public abstract boolean activateWindow(String screenId);

	public abstract boolean closeWindow(String screenId);

	public abstract Container getRootPane();

	public abstract void applicationClosing();

	public void openApplication(String title) {
		init();
		appicationWindow = new JFrame(title);
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		size.height = size.height - 50;
		size.width = size.width - 50;
		appicationWindow.setSize(size);
		appicationWindow.setLocation(25, 25);
		appicationWindow.setJMenuBar(menuContext.getMenuBar());
		// appicationWindow.getContentPane().add(getToolBar());
		appicationWindow.getContentPane().add(getRootPane());
		appicationWindow.addWindowListener(this);
		appicationWindow.setVisible(true);
	}

	public Container createView(UiModel model) {
		return builder.buildContainer(model);
	}

	public void close() {
		appicationWindow.setVisible(false);
		appicationWindow.dispose();
	}

	public boolean isViewOpened(String screenId) {
		return openViews.containsKey(screenId);
	}

	public void windowActivated(WindowEvent arg0) {

	}

	public void windowClosed(WindowEvent arg0) {
		System.exit(0);
	}

	public void windowClosing(WindowEvent arg0) {
		applicationClosing();
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent arg0) {

	}

	public void windowDeiconified(WindowEvent arg0) {

	}

	public void windowIconified(WindowEvent arg0) {

	}

	public void windowOpened(WindowEvent arg0) {

	}

	public MenuContext getMenuContext() {
		return menuContext;
	}

	public void setMenuContext(MenuContext menuContext) {
		this.menuContext = menuContext;
	}

	public ContainerFactory getBuilder() {
		return builder;
	}

	public void setBuilder(ContainerFactory builder) {
		this.builder = builder;
	}

}
